﻿using StackExchange.Redis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Publicuse.DBCore.Redis.CMultiplexer
{
   /// <summary>
   /// Redis连接管理
   /// </summary>
    public sealed class CMultiplexerConnectionHelper
    {
        private static readonly ConcurrentDictionary<int, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<int, ConnectionMultiplexer>();

        private static CMultiplexerConnectionHelper redisConnectionHelpInstance;

        static CMultiplexerConnectionHelper()
        {
            redisConnectionHelpInstance = new CMultiplexerConnectionHelper();
        }

        public static CMultiplexerConnectionHelper GetInstance()
        {
            return redisConnectionHelpInstance;
        }

        public ConnectionMultiplexer GetConnectionMultiplexer(int key, ConfigurationOptions connectionString)
        {
            ConnectionMultiplexer cml = null;
            if (!ConnectionCache.ContainsKey(key))
            {
                cml = GetManager(key, connectionString);
            }
            else
            {
                if (ConnectionCache[key].IsConnected)
                    cml = ConnectionCache[key];
                else
                    cml = GetManager(key, connectionString);
            }

            return cml;
        }

        public ConnectionMultiplexer GetManager(int key, ConfigurationOptions connectionString)
        {
            var connect = ConnectionMultiplexer.Connect(connectionString);

            connect.ConnectionFailed += MuxerConnectionFailed;
            connect.ConnectionRestored += MuxerConnectionRestored;
            connect.ErrorMessage += MuxerErrorMessage;
            connect.ConfigurationChanged += MuxerConfigurationChanged;
            connect.HashSlotMoved += MuxerHashSlotMoved;
            connect.InternalError += MuxerInternalError;

            ConnectionCache.GetOrAdd(key, connect);
            return connect;
        }

        #region 事件

        /// <summary>
        /// 配置更改时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
        {
        }

        /// <summary>
        /// 发生错误时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
        {
        }

        /// <summary>
        /// 重新建立连接之前的错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
        {
        }

        /// <summary>
        /// 连接失败 ， 如果重新连接成功你将不会收到这个通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
        {
        }

        /// <summary>
        /// 更改集群
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
        {

        }

        /// <summary>
        /// redis类库错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MuxerInternalError(object sender, InternalErrorEventArgs e)
        {
        }

        #endregion 事件
    }
}
